package com.superteam.zzl.viewmodel.DynamicFragment;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
import com.superteam.zzl.activity.AuthorActivity;
import com.superteam.zzl.activity.R;
import com.superteam.zzl.activity.databinding.ActivityAuthorBinding;
import com.superteam.zzl.activity.implement.IOneselfActivityView;
import com.superteam.zzl.adapter.AuthorActivityAdapter;
import com.superteam.zzl.custom.DividerItemDecoration;
import com.superteam.zzl.model.DynamicFragment.MContentInfo;
import com.superteam.zzl.model.DynamicFragment.bean.Dynamic;

import java.util.List;

import jp.wasabeef.glide.transformations.CropCircleTransformation;

/**
 * Created by Administrator on 2017/1/10.
 */

public class ContentInfoPresenter {

    private ActivityAuthorBinding db;
    private Context context;
    private IOneselfActivityView iav;
    private MContentInfo mContentInfo;
    private Dynamic.DataBean.TrendsListBean trends;
    private AuthorActivityAdapter adapter;

    public ContentInfoPresenter(Context context, IOneselfActivityView iav, ActivityAuthorBinding db) {
        this.context = context;
        this.db = db;
        this.iav = iav;
        mContentInfo = new MContentInfo();
    }


    public void setData(Dynamic.DataBean.TrendsListBean trends) {
        this.trends = trends;
    }


    public Dynamic.DataBean.TrendsListBean getData() {
        return trends;
    }

    public void init() {
        adapter = new AuthorActivityAdapter(context, trends.image_list, this);
    }

    public void initView() {
        db.setTrends(trends);
        db.tl.setNavigationIcon(R.drawable.back1);
        db.tl.setTitle("");
        BitmapPool bitmapPool = Glide.get(context).getBitmapPool();
        Glide.with(context).load(trends.author.avatar).bitmapTransform(new CropCircleTransformation(bitmapPool)).into(db.avatar);
        if (trends.geoinfo.length() > 0) {
            db.geoinfo.setVisibility(View.VISIBLE);
            db.geoinfo.setText(trends.geoinfo);
        } else {
            db.geoinfo.setVisibility(View.GONE);
        }

        List<Dynamic.DataBean.TrendsListBean.TagsBean> tags = trends.getTags();
        if (tags.size() != 0) {
            String tagName = "";
            for (com.superteam.zzl.model.DynamicFragment.bean.Dynamic.DataBean.TrendsListBean.TagsBean tag : tags) {
                tagName = tagName + tag.tag_name + " ";
            }
            db.tagName.setText(tagName);
        }

        db.rv.setLayoutManager(new LinearLayoutManager(context));
        db.rv.addItemDecoration(new DividerItemDecoration(context, DividerItemDecoration.VERTICAL_LIST));
        String content = getContent(trends);
        if (content != null) {
            db.date.setVisibility(View.VISIBLE);
            db.date.setText(content);
        } else {
            db.date.setVisibility(View.GONE);
        }

        db.header.attachTo(db.rv, true);
        db.rv.setAdapter(adapter);
    }

    public AuthorActivity getActivity() {
        return (AuthorActivity) context;
    }
    public String getContent(com.superteam.zzl.model.DynamicFragment.bean.Dynamic.DataBean.TrendsListBean trends) {
        String travel_start = trends.getTravel_start();
        String travel_end = trends.getTravel_end();
        StringBuilder stringBuilder = getDay(travel_start, travel_end);
        if (trends.travel_place_name != null && trends.travel_place_name.length() > 0) {
            if (stringBuilder == null) {
                stringBuilder = new StringBuilder();
            }
            stringBuilder.append("去" + trends.travel_place_name + " ");
        }

        return stringBuilder == null ? null : stringBuilder.toString();
    }

    private StringBuilder getDay(String travelStart, String travelEnd) {
        String[] start = travelStart.split(" ")[0].split("-");
        String[] end = travelEnd.split(" ")[0].split("-");
        int Month = Integer.valueOf(end[1]) - Integer.valueOf(start[1]);
        int day = Integer.valueOf(end[2]) - Integer.valueOf(start[2]);
        StringBuilder stringBuilder = null;

        if (day > 0 || Month > 0) {
            stringBuilder = new StringBuilder(start[1] + "月" + start[2] + "日 ");
            if (Month > 0) {
                if (day < 0) {
                    switch (Month) {
                        case 1:
                        case 3:
                        case 5:
                        case 7:
                        case 8:
                        case 10:
                        case 12:
                            day = Integer.valueOf(end[2]) + 31 - Integer.valueOf(start[2]);
                            break;
                        case 2:
                            Integer integer = Integer.valueOf(start[0]);
                            if (integer % 4 == 0 && integer % 100 != 0 || integer % 400 == 0) {
                                day = Integer.valueOf(end[2]) + 29 - Integer.valueOf(start[2]);
                            } else {
                                day = Integer.valueOf(end[2]) + 28 - Integer.valueOf(start[2]);
                            }
                        case 4:
                        case 6:
                        case 9:
                        case 11:
                            day = Integer.valueOf(end[2]) + 30 - Integer.valueOf(start[2]);
                    }
                } else {
                    stringBuilder.append(Month + "个月 ");
                }
            }

            if (day > 0) {
                stringBuilder.append(day + "天 ");
            }
        }
        return stringBuilder;
    }

    public RecyclerView getRecyclerView() {
        return db.rv;
    }
}
